--[[
Copyright:www.vxinyou.com
Author:Hongru
CreateTime:2014-12-11
Description:对 cocoStudio 的 ScrollView 类重新封装了接口，实现回调， 并实现状态（args）记录功能
]]

local clsUIScrollView = Class("lua/ccs/clsUINode.lua"):Inherit()


local SCROLLVIEW_DIR = {

    none = 0,
    vertical = 1,
    horizontal = 2,
    both = 3,
	
}

local ScrollviewEventType = {

    SCROLLVIEW_EVENT_SCROLL_TO_TOP = 0,
    SCROLLVIEW_EVENT_SCROLL_TO_BOTTOM = 1,
    SCROLLVIEW_EVENT_SCROLL_TO_LEFT = 2,
    SCROLLVIEW_EVENT_SCROLL_TO_RIGHT = 3,
    SCROLLVIEW_EVENT_SCROLLING = 4,
    SCROLLVIEW_EVENT_BOUNCE_TOP = 5,
    SCROLLVIEW_EVENT_BOUNCE_BOTTOM = 6,
    SCROLLVIEW_EVENT_BOUNCE_LEFT = 7,
    SCROLLVIEW_EVENT_BOUNCE_RIGHT = 8,
	
}

function clsUIScrollView:create(args)
	
	self._root = ScrollView:create()
	self._root:setTouchEnabled(true)
	self._root:setBounceEnabled(true)
	self._root:setClippingEnabled(true)
	self._uiLayer = TouchGroup:create()
	self._uiLayer:addWidget(self._root)
	
	local c = clone(args)
	
	if c.width and c.height then
		self._root:setSize(CCSize(c.width,c.height))
		self._width = c.width
		self._height = c.height
	end
	
	if c.res then
		self._root:setBackGroundImage(getResPath() .. c.res)
	end
	
	if c.innerWidth and c.innerHeight then
		self._root:setInnerContainerSize(CCSize(c.innerWidth,c.innerHeight))
		self._innerWidth = c.width
		self._innerHeight = c.height
	end
	
	if c.dir then
		self._root:setDirection(c.dir)
		self._dir = c.dir
	end
end

function clsUIScrollView:attach(obj)
	assert(obj)
	self._root = obj
	self:addTouchEventListener()
end

function clsUIScrollView:doDistinctConfig()
	if not self.args then
		return 
	end
	if self.args.dir then
		self:setDirection(self.args.dir)
	end
	if self.args.w and self.args.h then
		self:setSize(self.args.w, self.args.h)
	end
	if self.args.icw and self.args.ich then
		self:setInnerContainerSize(self.args.icw, self.args.ich)
	end
	if self.args.touchEnabled then
		self:setTouchEnabled(self.args.touchEnabled)
	end
	if self.args.bounceEnabled then
		self:setBounceEnabled(self.args.bounceEnabled)
	end
	if self.args.BGImage then
		self:setBackGroundImage(self.args.BGImage)
	end
	if self.args.clippingEnabled then
		self:setClippingEnabled(self.args.clippingEnabled)
	end
end

function clsUIScrollView:setSelfConfig(Type, newConfig)
	assert(Type)
	assert(newConfig)
	local configType = string.lower(Type)
	if configType == "direction" then
		self.args.dir = newConfig.dir
	elseif configType == "size" then
		self.args.w = newConfig.w
		self.args.h = newConfig.h
	elseif configType == "innercontainersize" then
		self.args.icw = newConfig.icw
		self.args.ich = newConfig.ich
	elseif configType == "touchenabled" then
		self.args.touchEnabled = newConfig.touchEnabled
	elseif configType == "bounceEnabled" then
		self.args.bounceEnabled = newConfig.bounceEnabled
	elseif configType == "backgroundimage" then
		self.args.BGImage = newConfig.BGImage
	elseif configType == "clippingenabled" then
		self.args.clippingEnabled = newConfig.clippingEnabled
	end
end

function clsUIScrollView:onScrollEventToTop()
	MOD_DEBUG.d('onScrollEventToTop')
end

function clsUIScrollView:onScrollEventToBootom()
	MOD_DEBUG.d('onScrollEventToBootom')
end

function clsUIScrollView:onScrollEventToLeft()
	MOD_DEBUG.d('onScrollEventToLeft')
end

function clsUIScrollView:onScrollEventToRight()
	MOD_DEBUG.d('onScrollEventToRight')
end

function clsUIScrollView:onScrollEventScrolling()
	MOD_DEBUG.d('onScrollEventScrolling')
end

function clsUIScrollView:onScrollEventBounceTop()
	MOD_DEBUG.d('onScrollEventBounceTop')
end

function clsUIScrollView:onScrollEventBounceBottom()
	MOD_DEBUG.d('onScrollEventBounceBottom')
end

function clsUIScrollView:onScrollEventBounceLeft()
	MOD_DEBUG.d('onScrollEventBounceLeft')
end

function clsUIScrollView:onScrollEventBounceRight()
	MOD_DEBUG.d('onScrollEventBounceRight')
end

--Function:添加回调事件
function clsUIScrollView:addTouchEventListener()
	local function scrollViewEvent(sender,eventType)		
		if eventType == ScrollviewEventType.SCROLLVIEW_EVENT_SCROLL_TO_TOP then		
			self:onScrollEventToTop()
		elseif eventType == ScrollviewEventType.SCROLLVIEW_EVENT_SCROLL_TO_BOTTOM then
			self:onScrollEventToBootom()
		elseif eventType == ScrollviewEventType.SCROLLVIEW_EVENT_SCROLL_TO_LEFT then
		    self:onScrollEventToLeft()
		elseif eventType == ScrollviewEventType.SCROLLVIEW_EVENT_SCROLL_TO_RIGHT then
			self:onScrollEventToRight()
		elseif eventType == ScrollviewEventType.SCROLLVIEW_EVENT_SCROLLING then
			self:onScrollEventScrolling()
		elseif eventType == ScrollviewEventType.SCROLLVIEW_EVENT_BOUNCE_TOP then
			self:onScrollEventBounceTop()
		elseif eventType == ScrollviewEventType.SCROLLVIEW_EVENT_BOUNCE_BOTTOM then
			self:onScrollEventBounceBottom()
		elseif eventType == ScrollviewEventType.SCROLLVIEW_EVENT_BOUNCE_LEFT then
			self:onScrollEventBounceLeft()
		elseif eventType == ScrollviewEventType.SCROLLVIEW_EVENT_BOUNCE_RIGHT then
			self:onScrollEventBounceRight()
		end
	end
	assert(self._root)
	self._root:addTouchEventListener(scrollViewEvent)
end

--Caution:_uiLayer响应touch事件
function clsUIScrollView:getCOObj()
	return self._uiLayer
end


function clsUIScrollView:setDirection(dir)
	self._root:setDirection(dir)
	self:setSelfConfig("Direction", {dir = dir})
end

function clsUIScrollView:getDirection(dir)
	return self._root:getDirection()
end

--Input:size:CCSize(width,height)
function clsUIScrollView:setSize(width,height)
	self._root:setSize(CCSize(width,height))
	self:setSelfConfig("Size", {w = width, h = height})
end

function clsUIScrollView:getSize()
	return self._root:getSize()
end

--Input:size:CCSize(width,height)
function clsUIScrollView:setInnerContainerSize(width,height)
	self._root:setInnerContainerSize(CCSize(width,height))
	self:setSelfConfig("InnerContainerSize", {icw = width, ich = height})
end


function clsUIScrollView:getInnerContainerSize()
	return self._root:getInnerContainerSize()
end

--Caution:注意返回的是父节点下的 boundingBox，跟父节点相关
function clsUIScrollView:boundingBox()
	return self._root:boundingBox()
end

--Input:flag为bool值
function  clsUIScrollView:setTouchEnabled(flag)
	self._root:setTouchEnabled(flag)
	self:setSelfConfig("TouchEnabled", {touchEnabled = flag})
end

function  clsUIScrollView:isTouchEnabled()
	return self._root:getTouchEnabled()
end

function clsUIScrollView:setBounceEnabled(flag)
	self._root:setBounceEnabled(flag)
	self:setSelfConfig("BounceEnabled", {bounceEnabled = flag})
end

function clsUIScrollView:isBounceEnabled()
	return self._root:isBounceEnabled()
end

function clsUIScrollView:setBackGroundImage(res)
	self._root:setBackGroundImage(res)
	self:setSelfConfig("BackGroundImage", {BGImage = res})
end

function clsUIScrollView:insertItem(item, config)
	if item and config then
		local c = clone(config)
		self._root:addChild(item)
		item:setAnchorPoint(ccp(0,0))	
		item:setPosition(ccp(c.x,c.y))
	end
end

--Caution:item为原生没有经过封装的对象，如果不是,则用item:getNode()或者item._root(不推荐),造成代码难以维护
function clsUIScrollView:deleteItem(item)
	if item then
		self._root:removeChild(item)
	end
end

function clsUIScrollView:deleteAllItems()
	if self._root then
		self._root:removeAllChildren()
	end
end

function clsUIScrollView:getNode()
	assert(self._root)
	return self._root
end

function clsUIScrollView:setClippingEnabled(flag)
	self._root:setClippingEnabled(flag)
	self:setSelfConfig("ClippingEnabled", {clippingEnabled = flag})
end

function clsUIScrollView:isClippingEnabled()
	return self._root:isClippingEnabled()
end

function clsUIScrollView:scrollToBottom(time, Battenuated)
	self._root:scrollToBottom(time, Battenuated)
end
 
function clsUIScrollView:scrollToTop(time,Battenuated)
	self._root:scrollToTop(time, Battenuated)
end
 
function clsUIScrollView:scrollToLeft(time, Battenuated)
	self._root:scrollToLeft(time, Battenuated)
end
 
function clsUIScrollView:scrollToRight(time, Battenuated)
	self._root:scrollToRight(time, Battenuated)
end
 
function clsUIScrollView:scrollToTopLeft(time, Battenuated)
	self._root:scrollToTopLeft(time, Battenuated)
end
 
function clsUIScrollView:scrollToTopRight(time, Battenuated)
	self._root:scrollToTopRight(time, Battenuated)
end
 
function clsUIScrollView:scrollToBottomLeft(time, Battenuated)
	self._root:scrollToBottomLeft(time, Battenuated)
end
 
function clsUIScrollView:scrollToBottomRight(time, Battenuated)
	self._root:scrollToBottomRight(time, Battenuated)
end
 
function clsUIScrollView:scrollToPercentVertical(percent, time, Battenuated)
	self._root:scrollToPercentVertical(percent, time, Battenuated)
end
 
function clsUIScrollView:scrollToPercentHorizontal(percent, time, Battenuated)
	self._root:scrollToPercentHorizontal(percent, time, Battenuated)
end	
 
function clsUIScrollView:scrollToPercentBothDirection(point, time, Battenuated)
	self._root:scrollToPercentBothDirection(point, time, Battenuated)
end

function getClass()
	return clsUIScrollView
end
